package com.zhuang.compare.service;

import com.zhuang.compare.bean.BeanInterface;
import com.zhuang.compare.bean.MileBean;
import com.zhuang.compare.bean.YardBean;
import com.zhuang.compare.enums.BeanEnum;

/**
 * Created by zhuanglei on 15/4/16.
 */
public class GenerateServiceImpl implements GenerateService{

    @Override
    public BeanInterface getModelBean(String beanType, float value){
        BeanInterface bean = getModelBean(beanType);
        bean.setValue(value);
        return bean;

    }


    private BeanInterface getModelBean(String beanName) {
        try {
            return (BeanInterface)Class.forName(getClazzNameByKeyName(beanName)).newInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (InstantiationException e) {
            e.printStackTrace();
            return null;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            return null;
        }
    }

    private String getClazzNameByKeyName(String keyName) {
        for (BeanEnum beanEnum : BeanEnum.values()){
            if (keyName.equalsIgnoreCase(beanEnum.getBeanType())){
                return beanEnum.getClazzName();
            }
        }

        return "";
    }
}
